﻿using System;
using System.Management.Automation;
using NETWORKLIST;

namespace BSonPosh.Network
{

	[Cmdlet(VerbsCommon.Set, "Network", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium, DefaultParameterSetName = "ById")]
	public class SetNetworkCommand : PSCmdlet
	{

        private Boolean NetFound = false;

		public SetNetworkCommand()
		{
			// default this to the non-existent "Any" so we can tell if it's been set
			Category = NetworkCategory.Any;
		}

		[Parameter(Position = 0, Mandatory = true, ParameterSetName = "ById", ValueFromPipelineByPropertyName = true)]
		[Alias("Id", "Guid")]
		public Guid NetworkId { get; set; }

        [Parameter(Position = 0, Mandatory = true,ParameterSetName = "ByName")]
		public string Name { get; set; }

        [Parameter(Position = 1)]
        public string NewName { get; set; }

		[Parameter(Position = 2)]
		[AllowEmptyString]
		public String Description { get; set; }

		[Parameter(Position = 3)]
		public NetworkCategory Category { get; set; }

		[Parameter]
		public SwitchParameter Force { get; set; }

		[Parameter]
		public SwitchParameter Passthru { get; set; }

		protected override void ProcessRecord()
		{
			base.ProcessRecord();

			using (var manager = new NLM())
			{
                IEnumNetworks networks = manager.Manager.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_ALL);
                
                foreach (INetwork net in networks)
				{
                    WriteVerbose(String.Format("Found Network {0} with ID {1}",net.GetName(),net.GetNetworkId().ToString()));
                    NetworkInfo network = new NetworkInfo(net);
                    if (NetworkId == network.NetworkId || Name.Equals(network.Name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        WriteVerbose(NetworkId.ToString());
                        if (Category != NetworkCategory.Any && ShouldProcess(string.Format("{0} ({1})", network.Name, network.NetworkId), "Set Category"))
                        {
                            net.SetCategory((NLM_NETWORK_CATEGORY)Category);
                        }

                        if (Description != null && ShouldProcess(string.Format("{0} ({1})", network.Name, network.NetworkId), "Set Description"))
                        {
                            net.SetDescription(Description);
                        }

                        if (NewName != null && ShouldProcess(string.Format("{0} ({1})", network.Name, network.NetworkId), "Change Name"))
                        {
                            net.SetName(NewName);
                        }

                        if (Passthru)
                        {
                            WriteObject(network);
                        }
                        NetFound = true;
                    }
				}
			}
		}
        protected override void EndProcessing()
        {
            base.EndProcessing();
            if (!NetFound)
            {
                WriteObject("No Network Objects Found");
            }
        }
	}
}